package com.example.goodpet.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.example.goodpet.R;
import com.example.goodpet.home.homeRecommend.bean.Tips;

import java.util.List;

public class PetTipsAdapter extends DelegateAdapter.Adapter<PetTipsAdapter.ViewHolder> {
    private Context mContext;
    private Tips.ResultDTO result;
    private List<Tips.ResultDTO.TipsMainDTO> tipsMain;
    private List<Tips.ResultDTO.TipsHealthDTO> tipsHealth;
    private List<Tips.ResultDTO.TipsCatDTO> tipsCat;
    private List<Tips.ResultDTO.OtherDTO> other;
    private List<Tips.ResultDTO.TipsDogDTO> tipsDog;

    private int MAIN=1;
    private int HEALTH=2;
    private int CAT=3;
    private int DOG=4;
    private int OTHER=5;

    private int TYPE;

    //用来获取布局
    private LayoutInflater mLayoutInflater;


    public PetTipsAdapter(Context mContext, Tips.ResultDTO result ,int type) {
        this.mContext=mContext;
        this.result=result;
        this.TYPE=type;

        tipsMain = result.getTipsMain();
        tipsHealth = result.getTipsHealth();
        tipsCat = result.getTipsCat();
        tipsDog = result.getTipsDog();
        other = result.getOther();

        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new GridLayoutHelper(1);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PetTipsAdapter.ViewHolder(mLayoutInflater.inflate(R.layout.item_tips_content, null));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if(TYPE==MAIN){
            Tips.ResultDTO.TipsMainDTO tipsMainDTO = tipsMain.get(position);
            holder.tips_content_title.setText(tipsMainDTO.getId()+"."+tipsMainDTO.getName());
            holder.tips_content_main.setText("\t\t\t"+tipsMainDTO.getContent());
        }else if(TYPE==HEALTH){
            Tips.ResultDTO.TipsHealthDTO tipsHealthDTO = tipsHealth.get(position);
            holder.tips_content_title.setText(tipsHealthDTO.getId()+"."+tipsHealthDTO.getName());
            holder.tips_content_main.setText("\t\t\t"+tipsHealthDTO.getContent());
        }else if(TYPE==CAT){
                Tips.ResultDTO.TipsCatDTO tipsCatDTO = tipsCat.get(position);
                holder.tips_content_title.setText(tipsCatDTO.getId()+"."+tipsCatDTO.getName());
                holder.tips_content_main.setText("\t\t\t"+tipsCatDTO.getContent());
        }else if(TYPE==DOG){
                Tips.ResultDTO.TipsDogDTO tipsDogDTO = tipsDog.get(position);
                holder.tips_content_title.setText(tipsDogDTO.getId()+"."+tipsDogDTO.getName());
                holder.tips_content_main.setText("\t\t\t"+tipsDogDTO.getContent());
        }else if(TYPE==OTHER){
            Tips.ResultDTO.OtherDTO otherDTO = other.get(position);
            holder.tips_content_main.setText(otherDTO.getContent());
        }
    }

    @Override
    public int getItemCount() {
        if(TYPE==MAIN){
            return tipsMain.size();
        }else if(TYPE==HEALTH){
            return tipsHealth.size();
        }else if(TYPE==CAT){
            return tipsCat.size();
        }else if(TYPE==DOG){
            return tipsDog.size();
        }else if(TYPE==OTHER){
            return other.size();
        }
        return 0;
    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tips_content_title;
        private TextView tips_content_main;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tips_content_title=itemView.findViewById(R.id.tips_content_title);
            tips_content_main=itemView.findViewById(R.id.tips_content_main);
        }
    }
}
